define(['../../carrys/cityList','../../carrys/jsonList','../../../../js/lib/other/sweet-alert'],function(cityData,jsonList) {
    return ['$scope','$location', '$interval','$timeout','bizProxy', function($scope,$location,$interval,$timeout,bizProxy) {
        $scope.$emit("top-show",'true');//顶部显示
        $scope.$emit('menuTypeLight','mine'); 	//控制底部menu高亮
        $scope.$emit('bub-menu','true'); 	//隐藏底部menu
        $scope.$emit('flag-tag','8-1');//显示底部文字

        $scope.isslide_up = false;//默认擅长领域
        var caseTypeList = transData.storage_baseData.queryAdvFieldList.advFieldList;
        caseTypeList.forEach(function(e){
        	e.caseTypeName = e.advFieldName;
        	e.caseTypeId = e.advFieldId;
            e.sel = false;
        });
        var goodFieldNum;
        var caseTempTypeData = new Array();
        caseTempTypeList(caseTypeList);

        var provinceList = new Array();
        var cityListTemp = new Array();
        var caseTypeTempList = new Array();



        $scope.isidt_card_no = false;
        $scope.lsSlt = true,$scope.ptSlt = false,$scope.isslide_down = true;
        $scope.lawyerNo = "";
        $scope.inviteCode = "";
        $scope.seProvinceName = "请选择省份";
        $scope.seCityName = "请选择城市";
        $scope.caseTypeData = "";


        collateData();//整理列表数据
        //显示省份
        $scope.provinceData = provinceList;

        //显示城市
        $scope.clickCity = function(){
            var cityList = new Array();
            var provinceName = ($scope.provinceName).province;
            for(var i=0;i<cityListTemp.length;i++){
                if(cityListTemp[i].province == provinceName){
                    var tempData = {'city': cityListTemp[i].city};
                    cityList.push(tempData);
                }
            }
            $scope.cityData = cityList;
        }


        //选中普通用户
        $scope.clickPtFn = function(){
            $scope.lsSlt = false,$scope.ptSlt = true,$scope.isslide_down = false;
        }

        //选中律师用户
        $scope.clickLsFn = function(){
            $scope.lsSlt = true,$scope.ptSlt = false,$scope.isslide_down = true;
        }

        //点击显示擅长领域
        $scope.showField = function(){
            $scope.isslide_up = true;
            if(caseTypeTempList.length > 0){
                for(var i=0;i<caseTypeTempList.length;i++){
                    for(var j=0;j<caseTypeList.length;j++){
                        if(caseTypeTempList[i] == caseTypeList[j].caseTypeName){
                            caseTypeList[j].sel = true;
                        }
                    }
                }
                $scope.caseTypeList = caseTypeList;
            }else{
                $scope.caseTypeList = caseTypeList;
            }
        }

        //点击关闭擅长领域
        $scope.closeType = function(){
            $scope.isslide_up = false;
        }

        //选择案件类型
        $scope.selectType = function(flag){
            var num=0;
            for(var i=0;i<caseTypeList.length;i++){
                if(caseTypeList[i].sel){
                    num++
                }
            }
            if(caseTypeList[flag].sel){
                caseTypeList[flag].sel = false;
            }else{
                if(num<4){
                    caseTypeList[flag].sel = true;
                }
            }
        }

        //确认选择
        $scope.confirmBtn = function(){
            var tempFieldString = "";
            var tempArray = new Array();
            for(var i=0;i<caseTypeList.length;i++){
                if(caseTypeList[i].sel){
                    tempArray.push(caseTypeList[i].caseTypeName);
                    tempFieldString = tempFieldString + caseTypeList[i].caseTypeId + ",";
                }
            }
            $scope.isslide_up = false;
            goodFieldNum = tempFieldString.substring(0,tempFieldString.length-1);
            if(tempArray.length  < 1){
                $scope.caseTypeData = "";
            }else{
                caseTypeTempList = tempArray;
                $scope.caseTypeData = tempArray;
            }
        }

        //点击提交
        $scope.subConfirm = function(){
            if(! $scope.userName){
                $scope.$emit("promptAlertMsg","用户姓名不能为空");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }else if(! $scope.documentsNo){
                $scope.$emit("promptAlertMsg","证件号码不能为空");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }else if(!_YHC_util.IdentityCodeValid($scope.documentsNo) && $scope.idType === '身份证'){
                $scope.$emit("promptAlertMsg","无效身份证号");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }else if( !_YHC_util.isValidBusCode($scope.documentsNo) && $scope.idType === '营业执照'){
                $scope.$emit("promptAlertMsg","无效营业执照号");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }else if(!setStatus($scope.email,'email') && $scope.email){
                $scope.$emit("promptAlertMsg","邮箱格式不正确");
                $scope.$emit("showCollect",true);
                $timeout(function(){
                    $scope.$emit("showCollect",false);
                },transData.promptTime);
                return;
            }else{
                if($scope.lsSlt){
                    if(! $scope.lawyerNo){
                        $scope.$emit("promptAlertMsg","律师证号不能为空");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                        return;
                    }else if(!$scope.lawfirmName){
                        $scope.$emit("promptAlertMsg","律所名称不能为空");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                        return;
                    }else if(($scope.lawyerNo).length != 17 || isNaN($scope.lawyerNo *1)){
                        $scope.$emit("promptAlertMsg","律师证号格式不正确");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                        return;
                    }
                    subLsData();
                }else{
                    subPtData();
                }
            }
        }


        function setStatus(data,name){
            var flag = false;
            if(data != ""){
                if(name == 'email'){
                    if(!_YHC_util.fChkMail(data)){
                        flag = false;
                    }else{
                        flag = true;
                    }
                }
            }else{
                flag = true;
            }

            return flag;
        }

        //处理擅长领域
        function caseTempTypeList(data){
            for(var i=0;i<data.length; i++){
                caseTempTypeData[data[i].caseTypeId] = data[i];
            }
        }

        //调用代理提交律师用户完善资料
        function subLsData(){
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;

            bizInfo.userName = $scope.userName;
            if($scope.idType == "身份证"){
                bizInfo.idType = "1";
            }else if($scope.idType == "营业执照"){
                bizInfo.idType = "2";
            }
            bizInfo.idNo = $scope.documentsNo;
            bizInfo.email = $scope.email;
            bizInfo.lawyerNo = $scope.lawyerNo;
            bizInfo.lawfirmName = $scope.lawfirmName;
            bizInfo.inviteCode = $scope.inviteCode;
            if($scope.provinceName != "" && $scope.cityName != ""){
                bizInfo.advArea = ($scope.provinceName).province + "_" + ($scope.cityName).city;
            }else{
                bizInfo.advArea = $scope.seProvinceName + "_" + $scope.seCityName;
            }

            if(caseTypeTempList.length > 0){
                bizInfo.advField = goodFieldNum;
            }else{
                bizInfo.advField = "";
            }

            console.log(bizInfo)
            $scope.$emit("loadingChange",true);
            bizProxy.queryProxyBizData("userExt4Lawyer",headerInfo,bizInfo,
                function(tempData){//SUCCESS
                    $timeout(function(){
                        console.log(tempData)
                        $scope.$emit("loadingChange",false);
                        assignmentData(tempData);
                        var tempBody = JSON.parse(tempData.body);
                        $scope.$emit("goNext","/mineLs");
                    },0);
                },function(data){
                    console.log(data)
                    if(data != ""){//FAIL
                        $scope.$emit("loadingChange",false);
                        if(data.errorCode == ERROR_CODE.noLogin){
                            FIREFLY("", data.errorMsg,"",function(){
                                //调用退出登录
                                transData.mobileNo = "";
                                transData.password = "";
                                transData.sessionId = "";
                                transData.userType = "";
                                resultData.loginUserData = "";
                                $timeout(function(){
                                    $scope.$emit("goNext","/mine");
                                },0);
                            });
                        }else{
                            $scope.$emit("loadingChange",false);
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                                if(transData.userType === '1'){
                                    $scope.$emit("goNext","/mineLs");
                                }else{
                                    $scope.$emit("goNext","/minePt");
                                }

                            },transData.promptTime);
                        }
                    }else{
                        $scope.$emit("loadingChange",false);
                        $scope.$emit("promptAlertMsg","连接超时");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                            if(transData.userType === '1'){
                                $scope.$emit("goNext","/mineLs");
                            }else{
                                $scope.$emit("goNext","/minePt");
                            }
                        },transData.promptTime);
                    }
                });
        }


        //调用代理提交普通用户完善资料
        function subPtData(){
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;

            bizInfo.userName = $scope.userName;
            if($scope.idType == "身份证"){
                bizInfo.idType = "1";
            }else if($scope.idType == "营业执照"){
                bizInfo.idType = "2";
            }

            bizInfo.idNo = $scope.documentsNo;
            bizInfo.email = $scope.email;

            console.log(bizInfo)
            $scope.$emit("loadingChange",true);
            bizProxy.queryProxyBizData("userExt",headerInfo,bizInfo,
                function(tempData){//SUCCESS
                    console.log(tempData)
                    $scope.$emit("loadingChange",false);
                    assignmentData(tempData);
                    $timeout(function(){
                        $scope.$emit("goNext","/minePt");
                    },100);
                },function(data){
                    console.log(data)
                    if(data != ""){//FAIL
                        $scope.$emit("loadingChange",false);
                        if(data.errorCode == ERROR_CODE.noLogin){
                            FIREFLY("", data.errorMsg,"",function(){
                                //调用退出登录
                                transData.mobileNo = "";
                                transData.password = "";
                                transData.sessionId = "";
                                transData.userType = "";
                                resultData.loginResultData = "";
                                resultData.loginUserData = "";
                                $timeout(function(){
                                    $scope.$emit("goNext","/mine");
                                },0);
                            });
                        }else{
                            $scope.$emit("loadingChange",false);
                            $scope.$emit("promptAlertMsg",data.errorMsg);
                            $scope.$emit("showCollect",true);
                            $timeout(function(){
                                $scope.$emit("showCollect",false);
                                if(transData.userType === '1'){
                                    $scope.$emit("goNext","/mineLs");
                                }else{
                                    $scope.$emit("goNext","/minePt");
                                }
                            },transData.promptTime);
                        }
                    }else{
                        $scope.$emit("loadingChange",false);
                        $scope.$emit("promptAlertMsg","连接超时");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                            if(transData.userType === '1'){
                                $scope.$emit("goNext","/mineLs");
                            }else{
                                $scope.$emit("goNext","/minePt");
                            }
                        },transData.promptTime);
                    }
                });
        }

        //监听跳过点击事件
        $scope.$on('skipEvent', function(event,data){
            if(data){
                if(transData.userType === '1'){
                    $scope.$emit("goNext","/mineLs");
                }else{
                    $scope.$emit("goNext","/minePt");
                }
            }
        });
        //整理省市数据
        function collateData() {
            cityListTemp = new Array();
            for(var key in cityData.addressDetail){
                var tempData = {"province":key};
                provinceList.push(tempData);
                collateCity(key,cityData.addressDetail[key]);
            }
        }
        function collateCity(data1,data2) {
            for(var key in data2){
                var tempList = {'province':data1,'city': key};
                cityListTemp.push(tempList);
            }
        }
        $scope.$apply();
    }];
});